package com.abcgz.grade.service;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Optional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.abcgz.grade.GradeException;
import com.abcgz.grade.dao.GradeRecord;
import com.abcgz.grade.dao.GradeRecordDao;
import com.abcgz.grade.dao.Innovate;
import com.abcgz.grade.dao.InnovateDao;
import com.abcgz.grade.dao.User;
import com.abcgz.grade.dao.UserDao;
import com.abcgz.grade.model.RestResult;

@Service
public class GradeService {
	
	@Autowired
	private UserDao userDao;
	
	@Autowired
	private InnovateDao innovateDao;
	
	@Autowired
	private GradeRecordDao gradeRecordDao;
	
	@Transactional
	public void submitGrade(GradeRecord rec) throws GradeException {
		Optional<Innovate> inno = this.innovateDao.findById(rec.getInnovateId());
		if(!inno.isPresent()) {
			throw new GradeException("数据不存在");
		}
		Innovate inn = inno.get();
		if(inn.getStatu() != 1) {
			throw new GradeException("已评分完毕");
		}
		GradeRecord has = this.gradeRecordDao.findByInnovateIdAndUsername(rec.getInnovateId(), rec.getUsername());
		if(has != null) {
			throw new GradeException("不可重复提交评分");
		}
		
		Double sum = 0.0;
		sum += rec.getScore1();
		sum += rec.getScore2();
		sum += rec.getScore3();
		sum += rec.getScore4();
		sum += rec.getScore5();
		sum += rec.getScore6();
		sum += rec.getScore7();
		if(sum > 100) {
			throw new GradeException("评分不可超过100分");
		}
		if(sum < 0) {
			throw new GradeException("评分不可小于0分");
		}
		rec.setScoreSum(sum);
		rec.setTimestamp(new Date().getTime());
		
		this.gradeRecordDao.save(rec);
		this.checkGradeComplete(inn);
	}
	
	public void checkGradeComplete(Innovate inn) {
		List<GradeRecord> records = this.gradeRecordDao.findByInnovateId(inn.getId());
		List<User> users = this.userDao.findAll();
		
		if(records.size() == users.size()) {
			Double[] scores = new Double[records.size()];
			Double min=Double.MAX_VALUE, max =Double.MIN_VALUE;
			int	minIndex = -1, maxIndex = -1;
			
			for(int i =0; i<records.size(); i++) {
				scores[i] = records.get(i).getScoreSum();
				if(scores[i] < min) {
					min = scores[i];
					minIndex = i;
				}
				if(scores[i] > max) {
					max = scores[i];
					maxIndex = i;
				}
			}

			Double sum = 0.0;
			for(int i =0; i<scores.length; i++) {
				if(i == minIndex || i == maxIndex) {
					continue;
				}
				sum += scores[i];
			}
			
			Double avg = 0.0;
			if(max == min || scores.length <= 2) {
				avg = scores[0];
			}
			else {
				avg = sum / (scores.length - 2);
			}
			avg = (double)Math.round(avg*100000)/100000;
			inn.setStatu(2);
			inn.setMaxScore(max);
			inn.setMinScore(min);
			inn.setOnscore(avg);
			if(inn.getAttscore() != null)
				avg=avg + inn.getAttscore();
			inn.setScore(avg);
			this.innovateDao.save(inn);
		}
		
//		if(records.size() == users.size()) {
//			Double min=Double.MAX_VALUE, max =Double.MIN_VALUE;
//			
//			Double sum = 0.0;
//			for(GradeRecord rec: records) {
//				sum += rec.getScoreSum();
//				if(rec.getScoreSum() < min) {
//					min = rec.getScoreSum();
//				}
//				if(rec.getScoreSum() > max) {
//					max = rec.getScoreSum();
//				}
//			}
//			
//			Double avg = 0.0;
//			avg = sum / (records.size());
//			avg = (double)Math.round(avg*10000)/10000;
//			
//			
//			inn.setStatu(2);
//			inn.setMaxScore(max);
//			inn.setMinScore(min);
//			inn.setScore(avg);
//			inn.setOnscore(avg);
//			this.innovateDao.save(inn);
//		}
	}
	
	public void enableGrade(Integer id) throws GradeException {
		Optional<Innovate> inno = this.innovateDao.findById(id);
		if(!inno.isPresent()) {
			throw new GradeException("创意作品不存在");
		}
		Innovate inn = inno.get();
		if(inn.getStatu() != 0) {
			throw new GradeException("作品正在评分或已评分完毕");
		}
		inn.setStatu(1);
		this.innovateDao.save(inn);
	}
}
